package com.fis.vaadin.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.vaadin.data.validator.AbstractValidator;

public class CheckFormatISNDValidator extends AbstractValidator
{
	private String regexp1 = "^01[0-9]{9}$";
	private String regexp2 = "^09[0-9]{8}$";
	private String regexp3 = "^9[0-9]{8}$";
	private String regexp4 = "^1[0-9]{9}$";
	Pattern pattern = null;
	Matcher matcher = null;
	
	public CheckFormatISNDValidator(String errorMessage)
	{
		super(errorMessage);
	}
	
	@Override
	public boolean isValid(Object value)
	{
		
		if ("".equals(value) || value == null)
		{
			return true;
		}
		String[] str = value.toString().trim().split(";");
		
		for (String val : str)
		{
			
			if (val.startsWith("9"))
			{
				pattern = Pattern.compile(regexp3);
			}
			else if (val.startsWith("1"))
			{
				pattern = Pattern.compile(regexp4);
			}
			else if (val.startsWith("09"))
			{
				pattern = Pattern.compile(regexp2);
			}
			else
			{
				pattern = Pattern.compile(regexp1);
			}
			matcher = pattern.matcher(val.trim());
			
			if (!matcher.matches())
			{
				return false;
			}
			
		}
		return true;
	}
	
}
